package com.gc.web.controller.business;

import cn.hutool.core.lang.Console;
import com.gc.business.domain.FinancialManagement;
import com.gc.business.service.IFinancialManagementService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.MoneyUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 财务管理 控制层
 * 
 * @author liujinteng
 * @date 2020-11-06
 */
@Slf4j
@Controller
@RequestMapping("/business/financialManagement")
public class FinancialManagementController extends BaseController {
    private String prefix = "business/financialManagement";
	
	private final IFinancialManagementService financialManagementService;

	@Autowired
	public FinancialManagementController(IFinancialManagementService financialManagementService) {
		this.financialManagementService = financialManagementService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:financialManagement:view")
	@GetMapping
	public String financialManagement(){
	    return prefix + "/financialManagement";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add/{financialId}")
	public String add(@PathVariable("financialId") Long financialId, ModelMap mmap){
		FinancialManagement financialManagement = financialManagementService.getById(financialId);
		mmap.put("financialManagement", financialManagement);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{financialId}")
	public String edit(@PathVariable("financialId") Long financialId, ModelMap mmap){
		FinancialManagement financialManagement = financialManagementService.getById(financialId);
		mmap.put("financialManagement", financialManagement);
		return prefix + "/edit";
	}
	
	/**
	 * 查询财务管理列表
	 */
	@RequiresPermissions("business:financialManagement:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(FinancialManagement financialManagement){
		startPage();
        List<FinancialManagement> list = financialManagementService.list(financialManagement);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存财务管理
	 */
	@RequiresPermissions("business:financialManagement:add")
	@Log(title = "财务管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(FinancialManagement financialManagement){
		FinancialManagement management = financialManagementService.getById(financialManagement.getFinancialId());
		if (management == null){
			return Result.failed("该款项不存在");
		}
		financialManagement.setReceiveAmount(management.getContractAmount());
		financialManagement.setUncollectAmount("0");
		financialManagementService.updateById(financialManagement);
		return Result.success();
	}

	/**
	 * 修改保存财务管理
	 */
	@RequiresPermissions("business:financialManagement:edit")
	@Log(title = "财务管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(FinancialManagement financialManagement){
		if (StringUtils.isEmpty(financialManagement.getReceiveAmount())){
			return Result.failed("请输入结算数量");
		}
		FinancialManagement management = financialManagementService.getById(financialManagement.getFinancialId());
		if (management == null){
			return Result.failed("该款项不存在");
		}
		if (MoneyUtils.moneyComp(financialManagement.getReceiveAmount(),management.getUncollectAmount())){
			return Result.failed("部分结算失败,结算金额超过未收款金额");
		}
		//部分结算后已收款金额
		String receiveAmount = MoneyUtils.moneyAdd(management.getReceiveAmount(),financialManagement.getReceiveAmount());
		//部分结算后未收款金额
		String unCollectAmount = MoneyUtils.moneySub(management.getUncollectAmount(),financialManagement.getReceiveAmount());
		financialManagement.setReceiveAmount(receiveAmount);
		financialManagement.setUncollectAmount(unCollectAmount);
		//部分结算后已收与未收金额合计
		String countAmount = MoneyUtils.moneyAdd(receiveAmount,unCollectAmount);
		Console.log("合计:"+countAmount+",已收款金额:"+receiveAmount+",未收款金额:"+unCollectAmount);
		if (!management.getContractAmount().equals(countAmount)){
			return Result.failed("部分结算失败,与合同金额不符");
		}
		financialManagementService.updateById(financialManagement);
		return Result.success();
	}
	
	/**
	 * 删除财务管理
	 */
	@RequiresPermissions("business:financialManagement:remove")
	@Log(title = "财务管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		financialManagementService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出财务管理列表
	 */
	@RequiresPermissions("business:financialManagement:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(FinancialManagement financialManagement){
		List<FinancialManagement> list = financialManagementService.list(financialManagement);
		ExcelUtil<FinancialManagement> util = new ExcelUtil<>(FinancialManagement.class);
		util.exportExcel(list, "financialManagement");
		return Result.success();
	}
}
